# 贷款申请

from django.shortcuts import render

from web.models import LoanOrder

from django.http import JsonResponse

from django.forms.models import model_to_dict

def filter_model_to_dict(data, template=0):
    """
    过滤model_to_dict
    :param data:
    :param template:
    :return:
    """
    data.repayment_time = data.repayment_time.strftime("%Y-%m-%d")
    new_data = model_to_dict(data)
    new_data["user_info"] = {
        "user_number": data.user.user_number,
        "name": data.user.name,
        "phone": data.user.phone,
        "user_avatar_img": data.user.user_avatar_img.url,
        "user_is_authenticated": data.user.user_is_authenticated,
        "loan_default_amount": data.user.loan_default_amount - data.amount
    }
    new_data["create_time"] = data.create_time.strftime("%Y-%m-%d")
    # 订单状态
    new_data["status"] = data.get_status_display()
    return new_data
def apply_list(request):
    if request.method == "POST":
        result = {
            "code": 0,
            "msg": "",
            "count": 0,
            "data": {
                "LoanOrder": []
            }
        }
        result["data"]["LoanOrder"] = [filter_model_to_dict(i) for i in LoanOrder.objects.filter(status=0)]
        result["count"] = len(result["data"]["LoanOrder"])
        return JsonResponse(result)
    return render(request, 'back/views/admin/loan/apply-list.html')


def loan_list(request):
    return render(request, 'back/views/admin/loan/loan-list.html')